import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import actions from './actions';
import mutations from './mutations';
import module1 from './modules/module1';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userInfo: null, // 用户信息
    vuexData: {
      desc: '这里的数据只存储在 vuex 中, 并没有缓存到 localStorage',
    },
  },
  getters: {
    isAuthenticated: (state) => !!state.userInfo?.token,
    isVip: (state) => state.userInfo?.level === 1,
  },
  mutations,
  actions,
  modules: {
    module1,
  },
  plugins: [
    createPersistedState({
      key: 'vue_element_pc', // 存储 key
      storage: window.localStorage, // 可选 sessionStorage
      // 指定需要缓存的状态
      // paths: ['userInfo', 'module1.name'],
      reducer(val) {
        return {
          userInfo: val.userInfo,
          module1: { name: val.module1.name },
        };
      },
    }),
  ],
});
